package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLTranPlanToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLTranPlanToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLTranPlanToF4QueryCmdImpl extends CommandAdapter implements ViewDLTranPlanToF4QueryCmd {
    private final static FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLTranPlanToF4Mapper viewDLTranPlanToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLTranPlanToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLTranPlanToF4QueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();

        if ("submit".equals(action)) {
            //提交
            List<HashMap> list = viewDLTranPlanToF4Mapper.selTranPlanData(ids, 2, null, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RATION");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "CHK");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("BILL_SOURCE", TypeUtils.castToInt(hashMap.get("BILL_SOURCE")));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("unsubmit".equals(action)) {
            //取消提交
            List<HashMap> list = viewDLTranPlanToF4Mapper.selTranPlanData(ids, 1, null, null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RATION");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "UNCHK");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("BILL_SOURCE", TypeUtils.castToInt(hashMap.get("BILL_SOURCE")));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("terminat".equals(action)) {
            //终止
            List<HashMap> list = viewDLTranPlanToF4Mapper.selTranPlanData(ids, null, null, 2);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RATION");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "TERMINAT");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("BILL_SOURCE", TypeUtils.castToInt(hashMap.get("BILL_SOURCE")));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("unterminat".equals(action)) {
            //取消终止
            List<HashMap> list = viewDLTranPlanToF4Mapper.selTranPlanData(ids, null, null, 1);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RATION");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "UNTERMINAT");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("BILL_SOURCE", TypeUtils.castToInt(hashMap.get("BILL_SOURCE")));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else if ("void".equals(action)) {
            //作废
            List<HashMap> list = viewDLTranPlanToF4Mapper.selTranPlanData(ids, null, "N", null);

            for (HashMap hashMap : list) {
                JSONObject terJson = new JSONObject();
                terJson.put("BILL_TYPE", "RATION");
                terJson.put("BILL_NO", hashMap.get("BILL_NO"));
                terJson.put("STATUS_TYPE", "DEL");
                terJson.put("OPR_ECODE", hashMap.get("MODIFIERNAME"));
                terJson.put("OPR_ENAME", hashMap.get("MODIFIERENAME"));
                terJson.put("OPR_DATE", hashMap.get("MODIFIEDDATE"));
                terJson.put("BILL_SOURCE", TypeUtils.castToInt(hashMap.get("BILL_SOURCE")));

                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(terJson,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);
            }
        } else {
            //TODO  未确定什么功能触发
            List<HashMap> list = viewDLTranPlanToF4Mapper.selTranPlanData(ids, null, null, null);
            for (HashMap hashMap : list) {
                Long id = TypeUtils.castToLong(hashMap.get("ID"));
                String billNo = TypeUtils.castToString(hashMap.get("BILL_NO"));

                Date billDate = TypeUtils.castToDate(hashMap.get("BILL_DATE") + "000000");
                String formatBillDate = DATE_FORMAT.format(billDate);

                hashMap.put("BILL_DATE", formatBillDate);

                List<HashMap> itemList = viewDLTranPlanToF4Mapper.selTranPlanItemData(id);
                JSONArray itemArray = new JSONArray();
                for (HashMap itemH : itemList) {
                    itemH.put("BILL_NO", billNo);
                    JSONObject itemjs = new JSONObject(itemH);
                    itemArray.add(itemjs);
                }

                JSONObject js = new JSONObject(hashMap);
                js.put("DL_B_TRAN_PLAN_ITEM", itemArray);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);

            }

        }


        vh.put("data", jsonArray);
        return vh;
    }


}
